//
//  HRUserScoresViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-9-3.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRUserScoresViewController.h"
#import "HRUserScoreTableViewCell.h"
#import "HRPointList.h"

@interface HRUserScoresViewController (){
    NSMutableArray *arrayTable;
}

@end

@implementation HRUserScoresViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        arrayTable = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"积分明细";
    
    [self loadPointsList];
}

-(void)loadPointsList{
    
    //隐藏无数据提示页面
    [self hideHud];
    
    [self showCustomLoadingHud:kLoading inView:nil];
    [HRInterfaceManager selectPointsItemsById:^(BOOL isSucceed, NSString *message, id data) {
        [self hideCustomLoadingHud];
        if (isSucceed) {
            HRPointList *list = (HRPointList*)data;
            [arrayTable removeAllObjects];
            [arrayTable addObjectsFromArray:list.list];
            [_tableView reloadData];
        }else if([message isEqualToString:NO_WIFI_ERROR_CODE]){
            [self showHudByType:HUDTYPE_NO_WIFI];
            return;
        }else{
            [self showTextHUD:message];
        }
        
        //显示无数据提示
        if ([arrayTable count]==0) {
            [self showHudByType:HUDTYPE_USER_SCORE];
        }
        
        
        //页面加载耗时
        [self analysisEnd:nil];
    }];
}

#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [arrayTable count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HRPointInfo *info = [arrayTable objectAtIndex:section];
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
    bgV.backgroundColor = [UIColor colorFromHexRGB:@"f0f0f0"];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,5 , 300, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text = info.createDate;
    label.textColor = [UIColor colorFromHexRGB:@"303030"];
    [bgV addSubview:label];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 29, bgV.bounds.size.width, 1)];
    line.backgroundColor = [UIColor colorFromHexRGB:@"dcdcdc"];
    [bgV addSubview:line];
    
    return bgV;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HRUserScoreTableViewCell";
    HRUserScoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HRUserScoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell configWithData:[arrayTable objectAtIndex:indexPath.section]];
    return cell;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - HRProgress Delegate methods
//马上去找
-(void)tapHUDButtonAction:(id)button{
    
    UIButton *theButton=(UIButton *)button;
    
    if ([theButton.titleLabel.text isEqualToString:@"重新加载"]) {
        [self loadPointsList];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


@end
